/**
 * Desc
 * @description Hope You Do Good But Not Evil
 * @copyright   Copyright 2014-2015 <ycchen@iwshop.cn>
 * @license     LGPL (http://www.gnu.org/licenses/lgpl.html)
 * @author      muqing <zmq2163@qq.com>
 * @package     Wshop
 * @link        http://www.iwshop.cn
 */


DataTableConfig.order = [[0, 'asc']];

requirejs(['jquery', 'util', 'datatables', 'bootstrap', 'companyLevel'], function($, util, dataTables,bootstrap,companyLevel) {
    $(function() {
        var dt = $('.dTable').dataTable(DataTableConfig).api();
    });

    var Id      = false;
    var delBtn  = false;

    $('#modal_modi_company_level .btn-primary').click(function () {
        var btn = $(this);
        var name = $('#uname').val();
        var data = $('#company_level_info').serializeJson();
        if (name !== '') {
            btn.html('处理中');
            companyLevel.modiCompanyLevel(Id, data, function (r) {
                if (r.ret_code === 0) {
                    $('#modal_modi_company_level').modal('hide');
                    location.reload();
                    delBtn.parents('tr').remove();
                    if ( Id > 0) {
                        util.Alert('保存成功');
                    } else {
                        util.Alert('添加成功');
                    }
                } else {
                    util.Alert('操作失败', true);
                }
            });
            btn.html('保存');
        }
    });

    $('#modal_modi_company_level').on('show.bs.modal', function (event) {
        delBtn = $(event.relatedTarget);
        Id = parseInt(delBtn.data('id'));
        if (Id > 0) {
            companyLevel.getInfo(Id, function (r) {
                if (r.ret_code === 0) {
                    $("input[name='utype']").val(r.ret_msg.utype);
                    $("input[name='uname']").val(r.ret_msg.uname);
                    $("input[name='return_percent']").val(r.ret_msg.return_percent);
                } else {
                    util.Alert('加载信息失败', true);
                }
            });
        } else {
            $('#utype').val('');
            $('#uname').val('');
            $('#return_percent').val('');
        }
    });


});

